package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class UpdatePasswordServlet
 */
@WebServlet("/UpdatePasswordServlet")
public class UpdatePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdatePasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    /**
     * Invokes the action "updatepassword.jsp"
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		RequestDispatcher rd = request.getRequestDispatcher("updatepassword.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when updating the password
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();  
        
		HttpSession session = request.getSession(true);
		
		/**
		 * Parameters obtained form
		 */
		String op=request.getParameter("oldpassword");
		String p1=request.getParameter("password1");
		String n=(String) session.getAttribute("username");
		String p=(String) session.getAttribute("password");
		  
	    
	    VisitorService service = new VisitorService();
	    
	    Visitor visitor = new Visitor();
	    visitor.setUserName(n);
	    visitor.setPassword(p1);
	    
	    try {
	    	/**
	    	 * If equals newpassword and oldpassword
	    	 */
	    	if(op.equals(p)){
	    		
	    		/**
	    		 * The password is updated
	    		 */
	    		service.changePassword(visitor);
	    		
	    		 /**
				 * Invokes the action "personalvisitor.jsp"
				 */
	    		request.getRequestDispatcher("personalvisitor.jsp").forward(request, response);
	    		session.setAttribute("password", visitor.getPassword());
	    		
	    	/**
	    	 * If not equals newpassword and oldpassword
	    	 */
	    	} else {
	    		
	    		request.setAttribute("error", "Unknown password, please try again");
	    		/**
				 * Invokes the action "updatepassword.jsp"
				 */
				request.getRequestDispatcher("updatepassword.jsp").forward(request, response);
	    	}
	    	
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    
	    out.close(); 
	    
	}

}
